﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.ApplicationServices
{
    public class NailServiceService : INailServiceService
    {
        private readonly IRepository<NailService> _nailServiceRepository;

        public NailServiceService(IRepository<NailService> nailServiceRepository)
        {
            Check.Require(nailServiceRepository != null);

            this._nailServiceRepository = nailServiceRepository;
        }

        /// <summary>
        /// Helps view all nail services on list view
        /// </summary>
        /// <returns>List of all nail services</returns>
        public IList<NailService> GetAllNailServices()
        {
            var nailServices = _nailServiceRepository.List();

            Check.Ensure(nailServices != null);

            return nailServices.ToList();
        }

        /// <summary>
        /// Helps view nail services for search view
        /// </summary>
        /// <param name="where">Lambda condition to get nail services</param>
        /// <returns></returns>
        public IList<NailService> GetNailServices(Expression<Func<NailService, bool>> @where)
        {
            var nailServices = _nailServiceRepository.List(@where);

            Check.Ensure(nailServices != null);

            return nailServices.ToList();
        }

        /// <summary>
        /// Helps view active nail services only
        /// </summary>
        /// <returns>All active nail services</returns>
        public IList<NailService> GetAllActiveNailServices()
        {
            var nailServices = _nailServiceRepository.List(x => x.IsActive);

            Check.Ensure(nailServices != null);

            return nailServices.ToList();
        }

        ///// <summary>
        ///// Filter nail services by name among active ones.
        ///// </summary>
        ///// <param name="filter">The filtered name</param>
        ///// <returns></returns>
        //public IEnumerable<NailService> FilterActiveNailServicesByName(string filter)
        //{
        //    return _nailServiceRepository.List(x => x.IsActive &&
        //                                    (filter == null || filter.Trim() == "" || x.ServiceName.ToLower().Contains(filter.Trim().ToLower())));
        //}

        /// <summary>
        /// Helps view details of an nail service
        /// </summary>
        /// <param name="id">Identity of nail service</param>
        /// <returns>NailService with specified identity</returns>
        public NailService GetNailService(int id)
        {
            Check.Require(id > 0);

            NailService nailService = _nailServiceRepository.Get(id);

            Check.Ensure(nailService != null);

            return nailService;
        }

        /// <summary>
        /// Insert an nail service from view
        /// </summary>
        /// <param name="nail service">NailService to be inserted</param>
        /// <returns>Number of records affected</returns>
        public int InsertNailService(NailService nailService)
        {
            Check.Require(nailService != null);

            _nailServiceRepository.Add(nailService);
            return _nailServiceRepository.SaveChanges();
        }

        /// <summary>
        /// Update an nail service from view
        /// </summary>
        /// <param name="nail service">NailService to be updated</param>
        /// <returns>Number of records affected</returns>
        public int UpdateNailService(NailService nailService)
        {
            Check.Require(nailService != null);

            _nailServiceRepository.Update(nailService);
            return _nailServiceRepository.SaveChanges();
        }

        /// <summary>
        /// Helps to inactive an nail service
        /// </summary>
        /// <param name="nail service">NailService to be inactive</param>
        /// <returns>Number of records affected</returns>
        public int DeActivateNailService(NailService nailService)
        {
            Check.Require(nailService != null);

            nailService.IsActive = false;
            _nailServiceRepository.Update(nailService);
            return _nailServiceRepository.SaveChanges();
        }
    }
}
